<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false"
		:close-on-press-escape="false" :fullscreen="true">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">
			<el-form-item prop="dictName" label="名称">
				<el-input v-model="dataForm.dictName" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item v-if="dataForm.pid === '0'" prop="dictType" label="类型">
				<el-input v-model="dataForm.dictType" placeholder="类型"></el-input>
			</el-form-item>
			<el-form-item v-if="dataForm.pid !== '0'" prop="dictValue" label="值">
				<el-input v-model="dataForm.dictValue" placeholder="值"></el-input>
			</el-form-item>
			<el-form-item prop="sort" label="排序">
				<el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序">
				</el-input-number>
			</el-form-item>
			<el-form-item prop="remark" label="备注">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		name: "DictAddOrUpdateComponent",
		data: function() {
			return {
				visible: false,
				dataForm: {
					id: '',
					pid: '0',
					dictName: '',
					dictType: '',
					dictValue: '',
					sort: 0,
					remark: ''
				}
			}
		},
		computed: {
			dataRule: function() {
				return {
					dictName: [{
						required: true,
						message: this.$t('validate.required'),
						trigger: 'blur'
					}],
					dictType: [{
						required: true,
						message: this.$t('validate.required'),
						trigger: 'blur'
					}],
					code: [{
						required: true,
						message: this.$t('validate.required'),
						trigger: 'blur'
					}],
					dictValue: [{
						required: true,
						message: this.$t('validate.required'),
						trigger: 'blur'
					}],
					sort: [{
						required: true,
						message: this.$t('validate.required'),
						trigger: 'blur'
					}]
				}
			}
		}
		/*,
		        beforeCreate: function () {
		            this = this;
		        }*/
		,
		methods: {
			init: function() {
				this.visible = true;
				this.$nextTick(function() {
					this.$refs['dataForm'].resetFields();
					if (this.dataForm.id) {
						this.getInfo();
					}
				});
			},
			// 获取信息
			getInfo: function() {
				this.$http.get('/sys/dict/' + this.dataForm.id).then(function(res) {
					if (res.data.code !== 0) {
						return this.$message.error(res.msg);
					}
					this.dataForm = _.merge({}, this.dataForm, res.data.data);
				}).catch(function() {});
			},
			// 表单提交
			dataFormSubmitHandle: _.debounce(function() {
				this.$refs['dataForm'].validate(function(valid) {
					if (!valid) {
						return false;
					}
					this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/dict', this.dataForm).then(
						function(res) {
							if (res.data.code !== 0) {
								return this.$message.error(res.msg);
							}
							this.$message({
								message: this.$t('prompt.success'),
								type: 'success',
								duration: 500,
								onClose: function() {
									this.visible = false;
									this.$emit('refresh-data-list');
								}
							});
						}).catch(function() {});
				});
			}, 1000, {
				'leading': true,
				'trailing': false
			})
		}
	}
</script>

<style scoped>

</style>
